#pragma once

#include "Base/Tests/Test.Application.h"

#undef  APP_NAME
#define APP_NAME	TestCheckLicense
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
public:
	typedef SHARED_POINTER_TYPE( APP_NAME )		TestCheckLicensePtr;

	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{
	}
	
	~APP_NAME ()
	{
	}

	void Update (float dt) override
	{
		TestApp::Update( dt );
	}

	void OnEnter () override
	{
		CheckLicense();
	}

private:
	void CheckLicense ()
	{
		GetPlatform()->CheckLicense(
			DelegateBuilder::Create( TestCheckLicensePtr(this), &APP_NAME::OnLicenseChecked ),
			"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWhvJPMkUHO2uw4fDRNsh0UvlPKNKk4ERj2k7uXeSL3z8rP9WrpXm3frr27mHzUror/Y3zU5a8OJFlmQC5F13l/5cTvKNLT96GbuMUBM6mSWFP8l7YAC0PbfyZG1TxnnNWmuu9THfqmwUyF8ZgqwRIkoJ2hnXe8kKyRtn0GyaLpJGXxbv7n3vJYR45abXTM1LVFDJ7aPFbstCB3O8tTFP4IT7+EvGXb9HTHwGDymAsHTYEWybl/2GJPlgXdhdYu0HRXL8fSRk2izLJvedbu0ctCBcWmyCijFyxU2bTXzU6pqE7UH02kwAN+QGQXFjlzqz0oiNgCIyLsG0r3+fUfAmwIDAQAB"
		);
	}

	void OnLicenseChecked (ELicenseCheckResponse::type code, const string &error)
	{
		string	text = "Result: ";

		switch ( code )
		{
			case ELicenseCheckResponse::LICENSED :
				text << "LICENSED";
				break;

			case ELicenseCheckResponse::NOT_LICENSED :
				text << "NOT_LICENSED";
				break;

			case ELicenseCheckResponse::RETRY :
				text << "RETRY";
				break;

			case ELicenseCheckResponse::DETACHED_LISTENER :
				text << "DETACHED_LISTENER";
				break;

			case ELicenseCheckResponse::ERROR :
				text << "ERROR";
				break;

			case ELicenseCheckResponse::NO_INTERNET_CONNECTION :
				text << "NO_INTERNET_CONNECTION";
				break;

			default :
				text << "unknown";
				break;
		}

		text << "\n" << error;

		GetPlatform()->ShowMessage(
			DelegateBuilder::Create( TestCheckLicensePtr(this), &APP_NAME::OnMessageResult ),
			"License checked!", text, "OK", "Retry"
		);
	}

	void OnMessageResult (int result)
	{
		if ( result > 0 )
		{
			// do nothing
		}

		if ( result < 0 )
		{
			// retry
			CheckLicense();
		}
	}
};